package com.claude.mainactivity;

import org.json.JSONObject;

import com.android.volley.VolleyError;
import com.claude.bean.User;
import com.claude.bean.Version;
import com.claude.communication.RequestManager;
import com.claude.communication.RequestNode;
import com.claude.communication.RequestNode.RequestListener;
import com.claude.update.UpdateApp;
import com.claude.util.Check;
import com.claude.util.Configure;
import com.claude.util.Parser;
import com.special.ResideMenuDemo.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity{
	private String APP = "ATTENDANCE";
	private EditText mEditTextAccount = null;
	private EditText mEditTextPassword = null;
	private Button mButtonLogin = null;
	private MyApplication mMyApplication = null;
	private String mAccount = null;
	private String mPassword = null;
	private MyOnClickListener mMyOnClickListener = null;
	private SharedPreferences mSetting = null;
	private TextView mSaveTextViewAccount = null;
	private TextView mSaveTextViewPassword = null;
	private LinearLayout mLinearLayout = null;
	private RequestManager mRequestManager = null;	
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.activity_login);
		
		//set the Alpha of the login layout
		mLinearLayout = (LinearLayout)findViewById(R.id.loginbackground);
		mLinearLayout.getBackground().setAlpha(100);
		
		mMyApplication = (MyApplication) getApplication();
		mSetting = this.getSharedPreferences(APP, Context.MODE_WORLD_READABLE);
		this.saveInformation();
		this.initView();
	}
	
	public void onStart(){
		super.onStart();
	}
	
	public void onResume(){
		super.onResume();
	}
	
	public void onStop(){
		super.onStop();
		if(mRequestManager != null){
			mRequestManager.stopRequest();
		}
		LoginActivity.this.finish();
	}
	
	private void saveInformation(){
		mAccount = mSetting.getString("account", "");
		mPassword = mSetting.getString("password", "");
	}
	
	private void initView(){
		mEditTextAccount = (EditText) findViewById(R.id.ed1);
		mEditTextPassword = (EditText) findViewById(R.id.ed2);
		
		mSaveTextViewAccount = (TextView) findViewById(R.id.ed1);
		mSaveTextViewPassword = (TextView) findViewById(R.id.ed2);
		
		mSaveTextViewAccount.setText(mAccount);
		mSaveTextViewPassword.setText(mPassword);
		
		mButtonLogin = (Button) findViewById(R.id.btn);
		mMyOnClickListener = new MyOnClickListener();
		mButtonLogin.setOnClickListener(mMyOnClickListener);
	}
	
	
	
	private void turnToInitApp(){
		Intent intent = new Intent();
		intent.setClass(LoginActivity.this, InitAppActivity.class);
		LoginActivity.this.startActivity(intent);
	}
	
	
	
	class MyOnClickListener implements OnClickListener{

		public void onClick(View v) {
			mAccount = mEditTextAccount.getText().toString();
			mPassword = mEditTextPassword.getText().toString();
			User user = new User();
			user.setAccount(mAccount);
			user.setPassword(mPassword);
			mMyApplication.setUser(user);
			
			mSetting.edit().putString("account", mAccount).commit();
			mSetting.edit().putString("password", mPassword).commit();			
			turnToInitApp();
		}
	}
}
